package com.sensePlusVR.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
 * 
 * @author: Loren
 * @create: 2016-7-5上午10:23:14
 * @desc: 网络连接工具：是否有网络连接，WiFi连接，移动网络连接
 *
 */
public class NetworkUtils {
    /**
     * 无连接
     */
    public static final int STATE_CONNECT_NONE = 0;

    /**
     * WIFI连接
     */
    public static final int STATE_CONNECT_WIFI = 1;

    /**
     * 移动网络 2G/3G
     */
    public static final int STATE_CONNECT_MOBILE = 2;


    public static boolean isNetworkAvailable(Context context) {
        boolean bConnected = false;
        if (context == null) {
            return bConnected;
        }
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connManager != null) {
            NetworkInfo[] infos = connManager.getAllNetworkInfo();
            for (NetworkInfo info : infos) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    bConnected = true;
                    break;
                }
            }
        }
        return bConnected;
    }
    
    /**
     * 获取当前网络连接状态
     * 
     * @param context
     * @return 常量 STATE_CONNECT_NONE：无连接， STATE_CONNECT_WIFI：WIFI连接,
     *         STATE_CONNECT_MOBILE：移动网络 2G/3G
     */
	public static int getNetConnectState(Context context) {
        final ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (networkInfo != null && networkInfo.isConnected()) {
            return STATE_CONNECT_WIFI;
        }
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfo != null && networkInfo.isConnected()) {
            return STATE_CONNECT_MOBILE;
        }
        return STATE_CONNECT_NONE;
    }
	
    
    public static String getNetConnectStateStr(Context context) {
        String netWorkType = NETWORK_TYPE_UNKNOWN;

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            int type = networkInfo.getType();

            if (type == ConnectivityManager.TYPE_WIFI) {
                netWorkType = NETWORK_TYPE_WIFI;
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                netWorkType = getNetWorkClass(context);
            }
        }

        return netWorkType;
    }
    
    /**
     * Unknown network class
     */
    public static final int NETWORK_CLASS_UNKNOWN = 0;

    /**
     * wifi net work
     */
    public static final int NETWORK_WIFI = 1;

    /**
     * "2G" networks
     */
    public static final int NETWORK_CLASS_2_G = 2;

    /**
     * "3G" networks
     */
    public static final int NETWORK_CLASS_3_G = 3;

    /**
     * "4G" networks
     */
    public static final int NETWORK_CLASS_4_G = 4;
    
    
    public static final String NETWORK_TYPE_UNKNOWN = "unknown";
    
    public static final String NETWORK_TYPE_WIFI = "Wifi";
    
    public static final String NETWORK_TYPE_2G = "2G";
    
    public static final String NETWORK_TYPE_3G = "3G";
    
    public static final String NETWORK_TYPE_4G = "4G";
    
    
    public static String getNetWorkClass(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        switch (telephonyManager.getNetworkType()) {
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case TelephonyManager.NETWORK_TYPE_EDGE:
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_1xRTT:
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return NETWORK_TYPE_2G;

        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            return NETWORK_TYPE_3G;

        case TelephonyManager.NETWORK_TYPE_LTE:
            return NETWORK_TYPE_4G;

        default:
            return NETWORK_TYPE_UNKNOWN;
        }
    }
}
